package com.mzy.referencetest.cmdassociation;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mzy.javaapi.exception.ServerHandleException;
import lombok.extern.slf4j.Slf4j;
import org.testng.annotations.Test;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author mzy
 * @Date 2022/7/1 10:01
 * @Version 1.0
 */
@Slf4j
public class TestDemo {

    @Test
    public void test01(){

        IpFace ipFace = new IpFace();
        ipFace.setFaceName("ens33");
        ipFace.setIp("192.168.5.91");
        ipFace.setSubnet("24");
        ipFace.setTag("ip");
        disableVip(ipFace);
    }

    private void disableVip(IpFace ipFace){
        try {
            String param = String.format("sudo ip -4 addr del %s/%s dev %s", ipFace.getIp(), ipFace.getSubnet(), ipFace.getFaceName());
            System.out.println(param);
            cmd("/bin/sh", "-c",param);
        } catch (IOException e) {
            throw new ServerHandleException("删除VIP异常",e);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }


    public static CmdUtils.Msg cmd(String...cmd) throws IOException, InterruptedException {
        //超时时间60秒，编码utf-8
        CmdUtils.Msg msg = CmdUtils.execCmd(60, "utf-8", cmd);
        // 方式2：将对象转为json字符串
        String json2 = JSONObject.toJSONString(msg);
        log.info(json2);
        return msg;
    }
}
